import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import ElementUI from "element-ui";

Vue.use(VueRouter)

const routes = [

  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Register.vue')
  },
  {
    path: '/404',
    name: '404',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/404.vue')
  },
  {
    path: '/front',
    name: 'front',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/front/Front.vue'),
    children: [
      {path: 'home', name: 'FrontHome', component: () => import(/* webpackChunkName: "about" */ '../views/front/Home.vue')},
      {path: 'person', name: 'FrontPerson', component: () => import(/* webpackChunkName: "about" */ '../views/front/Person.vue')},
      {path: 'video', name: 'FrontVideo', component: () => import(/* webpackChunkName: "about" */ '../views/front/Video.vue')},
      {path: 'videoDetail', name: 'FrontVideoDetail', component: () => import(/* webpackChunkName: "about" */ '../views/front/VideoDetail.vue')},
      {path: 'article', name: 'FrontArticle', component: () => import(/* webpackChunkName: "about" */ '../views/front/Article.vue')},
      {path: 'articleDetail', name: 'FrontArticleDetail', component: () => import(/* webpackChunkName: "about" */ '../views/front/ArticleDetail.vue')},
      {path: 'doctor', name: 'FrontDoctor', component: () => import(/* webpackChunkName: "about" */ '../views/front/tm/Doctor.vue')},
      {path: 'booking', name: 'FrontBooking', component: () => import(/* webpackChunkName: "about" */ '../views/front/tm/Booking.vue')},
      {path: 'liveChat', name: 'FrontLiveChat', component: () => import(/* webpackChunkName: "about" */ '../views/front/tm/LiveChat.vue')},
      {path: 'videoChat', name: 'FrontVideoChat', component: () => import(/* webpackChunkName: "about" */ '../views/front/tm/VideoChat.vue')},
      {path: 'messageCenter', name: 'MessageCenter', component: () => import(/* webpackChunkName: "about" */ '../views/front/tm/MessageCenter.vue')},
      {path: 'medicalRecord', name: 'MedicalRecord', component: () => import(/* webpackChunkName: "about" */ '../views/front/tm/MedicalRecord.vue')},
      {path: 'map', name: 'Map', component: () => import(/* webpackChunkName: "about" */ '../views/front/Map.vue')}
    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}


//刷新页面会导致路由重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus")
  if (storeMenus) {

    //先获取当前的路由数据
    const currentRouteNames = router.getRoutes().map(v => v.name)
    if (!currentRouteNames.includes("Manage")) {
      //拼装路由
      const mangeRoute = {path: '/', name: "Manage", component: () =>  import(/* webpackChunkName: "about" */ '../views/Manage.vue'), redirect: "/home", children: [
          {path: 'person', name: '个人中心', component: () =>  import(/* webpackChunkName: "about" */ '../views/Person.vue')}
        ]}
      const menus = JSON.parse(storeMenus)
      menus.forEach(item => {
        if (item.path) {
          let itemMenu = {path: item.path.replace("/",""), name: item.name, component: () =>  import(/* webpackChunkName: "about" */ '../views/'+item.pagePath+'.vue'), children: []}
          mangeRoute.children.push(itemMenu)
        }else if(item.children.length) {
          item.children.forEach(item => {
            if (item.path) {
              let itemMenu = {path: item.path.replace("/",""), name: item.name, component: () =>  import(/* webpackChunkName: "about" */ '../views/'+item.pagePath+'.vue'), children: []}
              mangeRoute.children.push(itemMenu)
            }
          })
        }
      })

      router.addRoute(mangeRoute)
    }
  }
}
//再次重新设置路由
setRoutes()

// router.addRoute(
//     {
//       path: '/',
//       component: () =>  import(/* webpackChunkName: "about" */ '../views/Manage.vue'),
//       redirect: "/home",
//       children: [
//         {path: 'home', name: '首页', component: () =>  import(/* webpackChunkName: "about" */ '../views/Home.vue')},
//         {path: 'user', name: '用户管理', component: () =>  import(/* webpackChunkName: "about" */ '../views/User.vue')},
//         {path: 'role', name: '角色管理', component: () =>  import(/* webpackChunkName: "about" */ '../views/Role.vue')},
//         {path: 'menu', name: '菜单管理', component: () =>  import(/* webpackChunkName: "about" */ '../views/Menu.vue')},
//         {path: 'person', name: '个人中心', component: () =>  import(/* webpackChunkName: "about" */ '../views/Person.vue')},
//         {path: 'file', name: '文件管理', component: () =>  import(/* webpackChunkName: "about" */ '../views/File.vue')}
//       ]
//     }
// )

router.beforeEach((to,from,next) => {

  if (to.path === "/login") {
    next()
  }
  console.log("name:"+to.name)
  console.log("path:"+to.path)
  localStorage.setItem("currentPathName",to.name)
  store.commit("setPath")
  //未找到路由的情况
  if (!to.matched.length) {
    const storeMenus = localStorage.getItem("menus")
    if (storeMenus) {
      //存在menus，说明已经登录
      next("/404")
    }else {
      //没有登录，跳转到登录页面
      next("/login")
    }
  }
  next()
})

export default router
